<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="站点名称">
              <a-input v-model="queryParam.stationName" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="状态">
              <a-input v-model="queryParam.state" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.addStation.add()">新建</a-button>
      <a-button type="dashed" @click="tableOption">{{ optionAlertShow && '关闭' || '开启' }}提示</a-button>
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="remove">
            <remove-station ref="removeStation" :stationIds="selectedRowKeys.join(',')" @ok="handleOk"/>
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <s-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        showPagination="auto"
    >
        <span slot="state"
              slot-scope="text">
            <a-tag :color="text | stateColorFilter">{{ text | stateTextFilter }}</a-tag>
        </span>
      <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical"/>
            <remove-station ref="removeStation" :stationIds="String(record.stationId)" @ok="handleOk"/>
          </template>
        </span>
    </s-table>
    <add-station ref="addStation" @ok="handleOk"/>
    <edit-station ref="editStation" @ok="handleOk"/>
  </a-card>
</template>

<script>
import moment from 'moment'
import {STable} from '@/components'
import AddStation from './modules/station/AddStation'
import EditStation from './modules/station/EditStation'
import RemoveStation from './modules/station/RemoveStation'

const stateMap = {
  0: {
    color: "green",
    text: "待使用"
  },
  1: {
    color: "blue",
    text: "使用中"
  },
  2: {
    color: "yellow",
    text: "维修中"
  },
  3: {
    color: "red",
    text: "已报废"
  }
};

export default {
  name: 'BikeList',
  components: {
    STable,
    AddStation,
    EditStation,
    RemoveStation
  },
  data () {
    return {
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '自行车id',
          dataIndex: 'bikeId',
          align: 'center'
        },
        {
          title: '站点名称',
          dataIndex: 'stationName',
          align: 'center'
        },
        {
          title: '状态',
          dataIndex: 'state',
          align: 'center',
          scopedSlots: {customRender: "state"}
        },
        {
          title: '新建时间',
          dataIndex: 'gmtCreate',
          align: 'center',
          customRender: (text) => moment(text).format('YYYY-MM-DD')
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          align: 'center',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收
        const queryJson = {'queryParam': JSON.stringify(this.queryParam)}
        return queryBike(Object.assign(parameter, queryJson))
            .then(res => {
              console.log(res.result)
              return res.result
            })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: {
          show: true, clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  created () {
    this.tableOption()
  },
  filters: {
    stateTextFilter (type) {
      return stateMap[type].text
    },
    stateColorFilter (type) {
      return stateMap[type].color
    }
  },
  methods: {
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: {
            show: true, clear: () => {
              this.selectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleEdit (record) {
      this.$refs.editStation.edit(record)
    },
    handleOk () {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>
